<template>
  <div>
    <el-dialog title="添加解答题" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" size="mini">
        <el-form-item label="题目名称" :label-width="formLabelWidth">
          <el-input type="text" v-model="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="题号" :label-width="formLabelWidth">
          <el-input-number
            v-model="form.idRange[0]" controls-position="right" :min="1" :max="form.idRange[1]"/>
          -
          <el-input-number
            v-model="form.idRange[1]" controls-position="right" :min="form.idRange[0]"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { QUESTION_GROUP_TYPE_MAP, QUESTION_SIZE_MAP } from '@/assets/js/constant';
import { uuid as createUUID } from '@/assets/js/utils';
import CommonMixin from './common-mixin';

export default {
  mixins: [CommonMixin],
  computed: {
    submitData() {
      const uuid = createUUID();
      const groupType = QUESTION_GROUP_TYPE_MAP.ANSWER;
      const { title, idRange } = this.form;
      const [startId, endId] = idRange;
      const { titleHeight } = QUESTION_SIZE_MAP;
      const idList = [...new Array(endId - startId + 1)];
      const { itemHeight } = QUESTION_SIZE_MAP.answer;
      const contents = idList.map((item, index) => ({
        height: itemHeight,
        content: `${startId + index}.`,
      }));
      const height = titleHeight
        + contents.map(item => item.height).reduce((prev, curr) => prev + curr);
      const totalHeight = height;
      const noSplitHeight = itemHeight;

      return {
        title,
        uuid,
        groupType,
        startId,
        endId,
        titleHeight,
        partIndex: 0,
        height,
        totalHeight,
        contents,
        noSplitHeight,
      };
    },
  },
  methods: {
    resetFormData() {
      const beginId = this.getBeginId();
      return {
        title: '解答题',
        idRange: [beginId, beginId],
      };
    },
  },
};
</script>
